/**
 * 添加项目
 */
import query from '../../untils/mysql';
import * as uuid from 'uuid';
import { Tip } from '../../untils/untils';
import { check, validationResult } from 'express-validator';

const validator = [
    check('name').notEmpty().withMessage("项目名称不能为空！"),
]
const POST = async (req: any, res: any, next: any) => {
    try {
        const { name, projectPic, remarks } = req.body;
        const { userId } = req.auth;
        var errors = validationResult(req);
        if (!errors.isEmpty()) {
            return Tip.tipError(res, errors.array()[0].msg);
        }
        const projectID = uuid.v1();
        await query(`insert into project (id,name,create_id,project_pic,remarks) values ('${projectID}','${name}','${userId}','${projectPic}','${remarks}')`);
        await query(`insert into project_pople (id,project_id,pople_id) values ('${uuid.v1()}','${projectID}','${userId}')`);
        return Tip.tipSuccess(res, '新增成功')
    } catch (error: any) {
        Tip.tipError500(res, error)
    }
}

export {
    POST,
    validator
}